package com.tssf.car_manage.controller;

import com.tssf.car_manage.entity.Customer;
import com.tssf.car_manage.entity.Vehicle;
import com.tssf.car_manage.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {
    private final CustomerService customerService;

    @Autowired
    public CustomerController(CustomerService customerService) {
        this.customerService = customerService;
    }

    @GetMapping("/findAll")
    public String list(Model model) {
       List<Customer> customerList = customerService.findAll();
        System.out.println(customerList);
        model.addAttribute("customers", customerList);
        return "customer/list";
    }

//     添加客户表单 - 修正返回的视图路径
    @GetMapping("/toadd")
    public String addForm(Model model) {
//        log.info("Received request for /customer/add");
        model.addAttribute("customer", new Customer());
        return "customer/add";
    }
//     处理表单提交 (POST请求)
    @PostMapping("/add")  // 必须使用@PostMapping
    public String handleAddForm(@ModelAttribute Customer customer) {
        customerService.save(customer);
        return "redirect:/customer/list"; // 提交后重定向到列表页
    }



    @GetMapping("/list")
    public String listCustomers(Model model) {
        model.addAttribute("customers", customerService.findAll());
        return "customer/list";  // 确保list.html存在
    }
//    @PostMapping("/save")
//    public String save(Customer customer) {
//        customerService.save(customer);
//        return "redirect:/customer";
//    }

    // 编辑表单展示
    // 编辑表单展示 - 移除redirect前缀
    @GetMapping("/edit/{id}")
    public String showEditForm(@PathVariable Integer id, Model model) {
        Customer customer = customerService.findById(id);
        if (customer == null) {
            throw new RuntimeException("客户不存在");
        }
        model.addAttribute("customer", customer);
        return "customer/edit"; // 直接返回视图，不要用redirect
    }

    // 处理编辑表单提交 - 修改路径为/edit以匹配表单
    @PostMapping("/edit")
    public String updateCustomer(@ModelAttribute Customer customer,
                                 RedirectAttributes redirectAttributes) {
        try {
            int result = customerService.update(customer);
            if (result > 0) {
                redirectAttributes.addFlashAttribute("success", "客户更新成功");
            } else {
                redirectAttributes.addFlashAttribute("error", "更新失败，数据可能未变化");
            }
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("error", "更新失败: " + e.getMessage());
            return "redirect:/customer/edit/" + customer.getCustomer_id();
        }
        return "redirect:/customer/list";
    }
    // 删除客户
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable Integer id,Model model) {
        try {
            customerService.delete(id);
            return "redirect:/customer/list";
        } catch (Exception e) {
            model.addAttribute("errorMessage", "删除客户失败，请检查关联数据！");
            List<Customer> customerList = customerService.findAll();
            System.out.println(customerList);
            model.addAttribute("customers", customerList);
            return "customer/list";
        }
    }

    @GetMapping("/search")
    public String searchCustomers(@RequestParam("key") String keyword, Model model) {
        List<Customer> customers = customerService.searchByNameOrPhone(keyword);
        model.addAttribute("customers", customers);
        model.addAttribute("key", keyword);
        return "customer/list"; // 返回客户列表页面
    }
}